﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Small.Html.Common;
using Small.Html.Entities;
using Small.Html.Options;

namespace Small.Html.UI
{
  public static  class HtmlValidateBox
  {
      /// <summary>
      /// 验证框
      /// </summary>
      /// <typeparam name="TModel">模型</typeparam>
      /// <typeparam name="TProperty">属性</typeparam>
      /// <param name="helper"></param>
      /// <param name="expression">Func<T>表达式</param>
      /// <param name="valiDateBoxOptions">格式</param>
      /// <param name="id"></param>
      /// <param name="htmlAttributes"></param>
      /// <returns></returns>
      public static MvcHtmlString ValidateBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, ValiDateBoxOptions valiDateBoxOptions, string id, object htmlAttributes = null) 
      {


        
          HtmlModelItem htmlItem = HtmlModelHelper.GetProperties(helper, expression);
          return new MvcHtmlString(GenerateHtml(htmlItem, id, valiDateBoxOptions, htmlAttributes));
      }


      /// <summary>
      /// 验证框
      /// </summary>
      /// <typeparam name="TModel">模型</typeparam>
      /// <typeparam name="TProperty">属性</typeparam>
      /// <param name="helper"></param>
      /// <param name="expression">Func<T>表达式</param>
      /// <param name="valiDateBoxOptions">验证格式</param>
      /// <param name="htmlAttributes">HTML属性</param>
      /// <returns></returns>
      public static MvcHtmlString ValidateBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, ValiDateBoxOptions valiDateBoxOptions, object htmlAttributes = null)
      {

          return ValidateBoxFor(helper, expression, valiDateBoxOptions, null, htmlAttributes);
      }

      /// <summary>
      /// 验证框
      /// </summary>
      /// <param name="helper"></param>
      /// <param name="name">名称</param>
      /// <param name="value">值</param>
      /// <param name="id">ID</param>
      /// <param name="validateFormat">格式</param>
      /// <param name="htmlAttributes">HTML属性</param>
      /// <returns></returns>
      public static MvcHtmlString ValidateBox(this HtmlHelper helper, string name, string value, string id, ValiDateBoxOptions validateFormat, object htmlAttributes = null)
      {

          HtmlModelItem htmlItem = new HtmlModelItem { Name = name, Value = value };
          return new MvcHtmlString(GenerateHtml(htmlItem, id, validateFormat, htmlAttributes));
      }
      /// <summary>
      /// 验证框
      /// </summary>
      /// <param name="helper"></param>
      /// <param name="name">名称</param>
      /// <param name="value">值</param>
      /// <param name="validateFormat">格式</param>
      /// <param name="htmlAttributes">HTML属性</param>
      /// <returns></returns>
      public static MvcHtmlString ValidateBox(this HtmlHelper helper, string name, string value, ValiDateBoxOptions validateFormat, object htmlAttributes = null)
      {

          HtmlModelItem htmlItem = new HtmlModelItem { Name = name, Value = value };
          return new MvcHtmlString(GenerateHtml(htmlItem, name, validateFormat, htmlAttributes));
      }
      /// <summary>
      /// 验证框
      /// </summary>
      /// <param name="helper"></param>
      /// <param name="name">名称</param>
      /// <param name="validateFormat">格式</param>
      /// <param name="htmlAttributes">HTML属性</param>
      /// <returns></returns>
      public static MvcHtmlString ValidateBox(this HtmlHelper helper, string name, ValiDateBoxOptions validateFormat, object htmlAttributes = null)
      {

          HtmlModelItem htmlItem = new HtmlModelItem { Name = name, Value = "" };
          return new MvcHtmlString(GenerateHtml(htmlItem, name, validateFormat, htmlAttributes));
      }
    

      //生成HTML代码
      private  static string GenerateHtml(HtmlModelItem htmlItem, string id, ValiDateBoxOptions validateFormat, object htmlAttributes = null)
      {


          StringBuilder builder = new StringBuilder();
          builder.Append(String.Format("<input type=\"text\" name=\"{0}\" class=\"{1}\"  id=\"{2}\" value=\"{3}\"   {4}='true'", htmlItem.Name, validateFormat.Class,
             String.IsNullOrEmpty(id) ? htmlItem.Name : id,
              htmlItem.Value,
              validateFormat.ValidFlag));

          builder.Append("  " + validateFormat.GetMessage());
          builder.Append(BaseHelper.FormatGenerateAttribute(htmlAttributes));
          builder.Append(DataOptions(validateFormat));
          builder.Append(" />\r\n");
          return builder.ToString();
      
      }




      /// <summary>
      /// 配置的属性
      /// </summary>
      /// <param name="validateFormat"></param>
      /// <returns></returns>
      private static string DataOptions(ValiDateBoxOptions validateFormat)
      {
          StringBuilder builder = new StringBuilder();

          int flag = 0;

          builder.Append(" data-options=\"");
          if (validateFormat.Required)
          {
              builder.Append(validateFormat.GetRequired());
              flag++;
          }

          if (!String.IsNullOrEmpty(validateFormat.ValidType))
          {
              flag++;
              builder.Append(",validType:'" + validateFormat.ValidType + "'");
          }
          //其他节点
          if (!String.IsNullOrEmpty(validateFormat.OtherDataOptions))
          {
              flag++;
              builder.Append("," + validateFormat.OtherDataOptions);
          }



          builder.Append("\"");
          if (flag >= 1)
          {
              return BaseHelper.SubSymbal(builder);
          }
          else 
          {
              return null;
          }
      }

     
      

  }
}
